﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Xml.Serialization;
using PortalWeb.Aviation.Core.Entity;
using PortalWeb.Common;

namespace PortalWeb.Flight.Core.Entity
{
 
    /// <summary>
    /// 航空PNR信息
    /// </summary>
    [Serializable]
    public sealed class PnrBase : IXmlObject
    {
        /// <summary>
        /// 成功失败标志
        /// </summary>
        [XmlElement("code")]
        public string Code { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        [XmlElement("error")]
        public string Error { get; set; }

        /// <summary>
        /// PNR编码
        /// </summary>
        [XmlElement("pnr")]
        public string PnrCode { get; set; }

        /// <summary>
        /// 大编码信息
        /// </summary>
        [XmlElement("bigpnr")]
        public string Bigpnr { get; set; }

        /// <summary>
        /// PNR内容
        /// </summary>
        [XmlElement("pnrcontent")]
        public string RtContent { get; set; }

        /// <summary>
        /// PAT内容
        /// </summary>
        [XmlElement("patacontent")]
        public string PatContent { get; set; }

        /// <summary>
        /// 行程类型
        ///  </summary>
        [XmlElement("triptype")]
        public EnumTripType TripType { get; set; }

        /// <summary>
        /// 航班信息
        /// </summary>
        [XmlArray("segments")]
        [XmlArrayItem("segment")]
        public List<SegmentBase> SegmentInfos { get; set; }

        /// <summary>
        /// 乘机人信息
        /// </summary>
        [XmlArray("passengers")]
        [XmlArrayItem("passenger")]
        public List<PassengerBase> Passengers { get; set; }

        /// <summary>
        ///  航司二字吗
        /// </summary>
        [XmlArray("air")]
        public string AirCode { get; set; }

        [XmlArray("hkstatus")]
        public string HkStatus { get; set; }

        [XmlArray("passengerstatus")]
        public string PassengerStatus { get; set; }

        [XmlArray("ticketstatus")]
        public string TicketStatus { get; set; }


        public XElement ExportXml()
        {
            XElement xml = new XElement("pnrinfo");
            xml.SetElementValue("code", Code);
            XElement xElement;
            if (Error != null)
            {
                xElement = new XElement("error");
                xElement.Add(new XCData(Error));
                xml.Add(xElement);
            }
            if (RtContent != null)
            {
                xElement = new XElement("pnrcontent");
                xElement.Add(new XCData(RtContent));
                xml.Add(xElement);
            }
            if (PatContent != null)
            {
                xElement = new XElement("patacontent");
                xElement.Add(new XCData(PatContent));
                xml.Add(xElement);
            }


            xml.SetElementValue("patacontent", PatContent);
            xml.SetElementValue("pnr", PnrCode);
            xml.SetElementValue("bigpnr", Bigpnr);
            xml.SetElementValue("triptype", TripType);
            xml.SetElementValue("ticketstatus", TicketStatus);
            xml.SetElementValue("passengerstatus", PassengerStatus);
            xml.SetElementValue("hkstatus", HkStatus);
            xml.SetElementValue("air", AirCode);

            xml.SetElementValue("passengercount", Passengers.Count);
            XElement xmlPassengers = new XElement("passengers");
            foreach (var passengers in Passengers)
            {
                xmlPassengers.Add(passengers.ExportXml());
            }
            xml.Add(xmlPassengers);

            XElement xmlSegments = new XElement("segments");
            foreach (var segment in SegmentInfos)
            {
                xmlSegments.Add(segment.ExportXml());
            }
            xml.Add(xmlSegments);
            return xml;
        }

        public void ImportXml(XElement xml)
        {
            Code = xml.GetElementValue("code", Code);
            Error = xml.GetElementValue("error", Error);
            RtContent = xml.GetElementValue("pnrcontent", RtContent);
            PatContent = xml.GetElementValue("patacontent", PatContent);
            PnrCode = xml.GetElementValue("pnr", PnrCode);
            Bigpnr = xml.GetElementValue("bigpnr", Bigpnr);
            TripType = xml.GetElementValue("triptype", TripType);
            TicketStatus = xml.GetElementValue("ticketstatus", TicketStatus);
            PassengerStatus = xml.GetElementValue("passengerstatus", PassengerStatus);
            HkStatus = xml.GetElementValue("hkstatus", HkStatus);
            AirCode = xml.GetElementValue("air", AirCode);
            var xElement = xml.Element("passengers");
            if (xElement != null)
            {
                Passengers = new List<PassengerBase>();
                foreach (XElement subXml in xElement.Elements("passenger"))
                {
                    PassengerBase passenger = new PassengerBase();
                    passenger.ImportXml(subXml);
                    Passengers.Add(passenger);
                }
            }

            xElement = xml.Element("segments");
            if (xElement != null)
            {
                SegmentInfos = new List<SegmentBase>();
                foreach (XElement subXml in xElement.Elements("segment"))
                {
                    SegmentBase segment = new SegmentBase();
                    segment.ImportXml(subXml);
                    SegmentInfos.Add(segment);
                }
            }
        }


        public void ImportXml(string xml)
        {
            try
            {
                ImportXml(XElement.Parse(xml));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " xml：" + xml, ex);
            }
        }
    }
}
